package com.jetpackmvp.base

import android.os.Bundle
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.annotation.IntDef
import androidx.core.content.ContextCompat
import com.jetpackmvp.R
import com.jetpackmvp.internet.RetrofitMananger
import com.jetpackmvp.utils.LogUtil
import com.jetpackmvp.widgets.TitleBar


abstract class TitleBarActivity : BaseActivity() {

    @IntDef(View.VISIBLE, View.INVISIBLE, View.GONE)
    @Retention(AnnotationRetention.SOURCE)
    internal annotation class Visibility

    private var mLayoutMain: View? = null
    lateinit var mTitleBar: TitleBar
    private var mLayoutContent: LinearLayout? = null
    private var mContent: View? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LogUtil.e("title")
        mLayoutMain = layoutInflater.inflate(R.layout.title_bar_content, null)
        mLayoutContent =findViewById(R.id.titleBarContent)
        mTitleBar = findViewById(R.id.title_bar)
        mTitleBar.setLeftDrawable(R.drawable.ic_launcher_foreground)
        mTitleBar.setTitleBarLeftClick { finish() }
//        setContentView(getLayoutContentId())
//        initView(savedInstanceState)
    }

    override fun <T : View?> findViewById(id: Int): T =
        mLayoutMain?.findViewById<T>(id) ?: super.findViewById<T>(id)


    override fun setContentView(layoutResID: Int) {
        mContent = layoutInflater.inflate(layoutResID, null)
        mLayoutContent!!.addView(
            mContent, LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT
        )
        super.setContentView(mLayoutMain)
    }


    fun getRightImageView(): ImageView {
        return mTitleBar.ivRight
    }

    fun setTitleBarText(title: String) {
        mTitleBar.setTitleBarText(title)
    }

    fun setTitleBarColor(color: Int) {
        mTitleBar.setTitleBarColor(ContextCompat.getColor(mContext!!, color))
    }

    fun setTitleBarLeft(drawable: Int) {
        mTitleBar.setLeftDrawable(drawable)
    }

    fun setTitleBarRight(drawable: Int) {
        mTitleBar.setRightDrawable(drawable)
    }

    fun setTitleBarRightPadding(drawable: Int) {
        mTitleBar.setRightDrawablePadding(drawable)
    }

    fun setTitleBarLeft(leftText: String) {
        mTitleBar.setLeftText(leftText)
    }

    fun setTitleBarRight(rightText: String) {
        mTitleBar.setRightText(rightText)
    }

    fun setTitleBarRight(rightText: String, iconId: Int, iconAtLeft: Boolean) {
        mTitleBar.setRightTextAndIcon(rightText, iconId, iconAtLeft)
    }

    fun setTitleBarLeft(leftText: String, iconId: Int, iconAtLeft: Boolean) {
        mTitleBar.setLeftTextAndIcon(leftText, iconId, iconAtLeft)
    }

    fun setTitleBarLeftVisibility(@Visibility visibility: Int) {
        mTitleBar.setLeftVisibility(visibility)
    }

    fun setTitleBarRightVisibility(@Visibility visibility: Int) {
        mTitleBar.setRightVisibility(visibility)
    }

    fun setTitleBarVisibility(@Visibility visibility: Int) {
        mTitleBar.setVisibility(visibility)
    }

    fun setTitleBarLeftClick(onLeftClickListener: View.OnClickListener) {
        mTitleBar.setTitleBarLeftClick(onLeftClickListener)
    }

    fun setTitleBarRightClick(onRightClickListener: View.OnClickListener) {
        mTitleBar.setTitleBarRightClick(onRightClickListener)
    }

    fun setTitleBarTitleClick(onTitleClickListener: View.OnClickListener) {
        mTitleBar.setTitleBarTitleClick(onTitleClickListener)
    }

    fun getTitleBar(): TitleBar {
        return mTitleBar
    }

    fun setTitleBarRightFirstClick(onRightClickListener: View.OnClickListener) {
        mTitleBar.setTitleBarRightFirstClick(onRightClickListener)
    }

    fun setTitleBarRightFirstVisibility(@Visibility visibility: Int) {
        mTitleBar.setRightFirstVisibility(visibility)
    }

    fun setTitleBarRightFirst(drawable: Int) {
        mTitleBar.setRightFirstDrawable(drawable)
    }
}
